/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.sessinfo;

import java.text.SimpleDateFormat;
import org.json.JSONArray;
import org.json.JSONML;
import org.json.JSONObject;
import org.webos.java.base.Application;
import org.webos.java.base.Frame;
import org.webos.java.base.FrameType;
import org.webos.java.base.RemoteMessage;
import org.webos.java.base.Session;
import org.webos.java.base.workspace.Workspace;
import org.webos.java.root.Log;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.SessionManager;

/**
 *
 * @author kostya
 */



@FrameType(code="sess_list", jsClass="sessinfo.SessionList")
public class SessionList extends Frame{
    
    SessionsInfo app = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat("d-MM-yyyy HH:mm:ss");

    public SessionList(SessionsInfo aApplication, Workspace aWorkspace) {
        super(aApplication, aWorkspace);
        app = aApplication;
        setIcon("sessinfo_icon");
        setTitle("List of active sessions:");
    }

    @RemoteMessage
    public JSONObject acceptMessage(JSONObject aObject){
        try {
            if(aObject.optString("message", "").equals("")){
                return NetworkManager.getErrorObject(102, "Message can't be empty");
            }
            for (int i = 0; i < SessionManager.getSessionCount(); i++) {
                Session session = SessionManager.getSessionAt(i);
                if(aObject.optString("to", "").equalsIgnoreCase(session.getLogin())){
                    SessionsInfo otherApp = (SessionsInfo) session.getApplication(SessionsInfo.class);
                    if(otherApp!=null){
                        //Send message
                        otherApp.chatHistory.receiveMessage(app.getSession().getLogin(), aObject.optString("message", ""));
                        return null;
                    }else{
                        return NetworkManager.getErrorObject(103, "It seems user "+session.getLogin()+" doesn't have session manager started");
                    }
                }
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return NetworkManager.getErrorObject(101, "Can't find user");
    }

    @RemoteMessage
    public JSONObject refreshList(JSONObject aONObject){
        try {
            JSONArray arr = new JSONArray();
            for (int i = 0; i < SessionManager.getSessionCount(); i++) {
                Session session = SessionManager.getSessionAt(i);
                JSONObject object = new JSONObject();
                object.put("login", session.getLogin());
                object.put("latestActivity", dateFormat.format(
                        getApplication().getSession().normalizeDate(session.getLatestActivity())));
                arr.put(object);
            }
            JSONObject res = new JSONObject();
            res.put("sessions", arr);
            return res;
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return null;
    }

}
